// EigenOp.C

#include "EigenOp.H"

/*
EigenOp::EigenOp(
  const Teuchos::RCP<LOCA::GlobalData>& global_data,
  const Teuchos::RCP<LOCA::Parameter::SublistParser>& topParams,
  const Teuchos::RCP<Teuchos::ParameterList>& eigenParams)
{ }
*/

EigenOp::~EigenOp()
{
}

NOX::Abstract::Group::ReturnType
  EigenOp::save(
    Teuchos::RCP<std::vector <double> >& evals_r,
    Teuchos::RCP<std::vector <double> >& evals_i,
    Teuchos::RCP<NOX::Abstract::MultiVector>& evecs_r,
    Teuchos::RCP<NOX::Abstract::MultiVector>& evecs_i)
{
  std::vector<double>::const_iterator i;
  for(i=evals_r->begin(); i!=evals_r->end(); ++i){
    cerr << "An eigenvalue is " << (*i) << endl;
  }
  for(i=evals_i->begin(); i!=evals_i->end(); ++i){
    if (abs(*i) > 1.0e-4) {
      cerr << "Aborting: Eigenvalue has imaginary part." << endl;
      exit(0);
    }
  }
  if (!is_null(prev_r)) {
    // cerr << "prev eigenvals" << endl;
    for (int i = 0; i<4; i++) {
      if ((prev_r->at(i) * evals_r->at(i)) < 0) {
        cerr << "Sign change in eigenvalue " << i << endl
	     << "Writing (real part of) eigenvector to file and stopping" << endl;
	char filename[32];
	sprintf(filename, "evec%d.%4f", i, run_count);
	ofstream fout(filename, ios::trunc);
	fout.precision(15);

	NOX::Abstract::Vector& evec = (*evecs_r)[i];
	evec.print(fout);
	/*
	NOX::Epetra::Vector& epvec = (NOX::Epetra::Vector&) evec;
	for (int j=0; j < evec.length(); j++) {
          fout << evec[j] << endl;
	}
	*/

	fout.close();
	exit(0);
      }
    }
  } 
//  else cerr << "first time" << endl;


  run_count++;
  prev_r = evals_r;
  return NOX::Abstract::Group::Ok;
}
